package com.publicClass;

import java.util.List;
import java.util.stream.Collectors;

public class CRUD<T> {
    public boolean add(T newElement, List<T> sampleElement) {
        try {
            sampleElement.add(newElement);
            return true;
        } catch (Error e) {
            return false;
        }
    }
    public boolean update(T updateElement, T conditionElement, List<T> sampleElement) {
        for (int i = 0; i < sampleElement.size(); i++) {
            if (sampleElement.get(i) .equals(conditionElement)) {
                sampleElement.set(i,updateElement);
                return true;
            }
        }
        return false;
    }

    public boolean delete(T conditionElement, List<T> sampleElement) {
        for (T t : sampleElement) {
            if (t.equals(conditionElement)) {
                sampleElement.clear();
                return true;
            }
        }
        return false;
    }

    public void deleteAll(List<T> sampleElement) {
        for (int i = 0; i < sampleElement.size(); i++) {
            sampleElement.remove(i);
        }
    }

    public boolean find(T conditionElement,List<T> sampleElement) {
        for (T t : sampleElement) {
            if (t.equals(conditionElement)) {
                System.out.println(t.toString());
                return true;
            }
        }
        return false;
    }

    public void printElements(List<T> sampleElements) {
        for (T sampleElement : sampleElements) {
            System.out.println(sampleElement);
        }
    }


}

